package com.yutian.endecryption.aes.component;

import com.yutian.endecryption.aes.util.SymmetricEncoder;
import com.yutian.endecryption.config.Config;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * AES 加密工具
 *
 * @author zhang peng yang ming
 * @create 2017-07-31 10:14
 */
public class JTabbedAES extends JPanel implements MouseListener {
    private JButton encryption, decryption;
    private JTextField textField;
    private JTextArea textArea;

    public JTabbedAES () {
        setLayout(new BorderLayout());

        textField = new JTextField();
        textArea = new JTextArea();
        textArea.setLineWrap(true);        //激活自动换行功能
        textArea.setWrapStyleWord(true);    // 激活断行不断字功能
        textArea.setBorder(BorderFactory.createLineBorder(new Color(171, 173, 179), 1));
        JPanel textFieldWrap = new JPanel();
        textFieldWrap.setLayout(new BorderLayout());
        textFieldWrap.add(textField);
        textFieldWrap.setBorder(BorderFactory.createTitledBorder("待处理字符串"));
        JPanel textAreaWrap = new JPanel();
        textAreaWrap.setLayout(new BorderLayout());
        textAreaWrap.add(textArea);
        textAreaWrap.setBorder(BorderFactory.createTitledBorder("加解密后字符串"));
        encryption = new JButton(" 加密 ");
        decryption = new JButton(" 解密 ");

        add(textFieldWrap, BorderLayout.NORTH);
        add(textAreaWrap, BorderLayout.CENTER);

        JPanel buttonWrap = new JPanel();
        buttonWrap.add(encryption);
        buttonWrap.add(decryption);

        add(buttonWrap, BorderLayout.SOUTH);
        setBackground(Color.RED);

        encryption.addMouseListener(this);
        decryption.addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource().equals(encryption)) {
            textArea.setText(SymmetricEncoder.AESEncode(Config.KEY, textField.getText()));
        } else if (e.getSource().equals(decryption)) {
            textArea.setText(SymmetricEncoder.AESDecode(Config.KEY, textField.getText()));
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
